#include "../include/Personnage.h"
#include <iostream>

Personnage::Personnage(const std::string &fname, sf::Color couleur, sf::Vector2f startPos, int speed) :
m_speed(speed), m_startPos(startPos)
{
    m_image.LoadFromFile(fname);
    m_image.CreateMaskFromColor(couleur);


    for (int i = 1; i<=4; i++)
    {

        m_TapeB.PushFrame(Frame(&m_image, sf::Rect<int>(cote_Carre*(i+2)-cote_Carre,0,cote_Carre*(i+2),cote_Carre)));
        m_TapeG.PushFrame(Frame(&m_image, sf::Rect<int>(cote_Carre*(i+2)-cote_Carre,32,cote_Carre*(i+2),cote_Carre*2)));
        m_TapeD.PushFrame(Frame(&m_image, sf::Rect<int>(cote_Carre*(i+2)-cote_Carre,64,cote_Carre*(i+2),cote_Carre*3)));
        m_TapeH.PushFrame(Frame(&m_image, sf::Rect<int>(cote_Carre*(i+2)-cote_Carre,96,cote_Carre*(i+2),cote_Carre*4)));
        if(i<=2){
        m_Bas.PushFrame(Frame(&m_image, sf::Rect<int>(cote_Carre*i-cote_Carre, 0, cote_Carre*i, cote_Carre)));
        m_Gauche.PushFrame(Frame(&m_image, sf::Rect<int>(cote_Carre*i-cote_Carre, 32, cote_Carre*i, cote_Carre*2)));
        m_Droite.PushFrame(Frame(&m_image, sf::Rect<int>(cote_Carre*i-cote_Carre, 64, cote_Carre*i, cote_Carre*3)));
        m_Haut.PushFrame(Frame(&m_image, sf::Rect<int>(cote_Carre*i-cote_Carre, 96, cote_Carre*i, cote_Carre*4)));
        }
    }
    m_direction=DROITE;
    m_animation.SetFrameTime(0.1f);
    m_animation.Pause();
    m_animation.SetLoop(true);
    m_animation.SetCenter(0, 0);
    m_animation.SetPosition(m_startPos);
    Walking = false;
}

Personnage::~Personnage()
{
    //dtor
}

void Personnage::Update(float ElapsedTime)
{
    if (Hitting != 0) //Si il tape
    {
        switch ( m_direction)
        {

        case GAUCHE :
        if ( m_animation.GetAnim() != &m_TapeG) // On change l'animation
        m_animation.SetAnim(&m_TapeG);
        break;

        case DROITE :
        if ( m_animation.GetAnim() != &m_TapeD) // On change l'animation
        m_animation.SetAnim(&m_TapeD);
        break;

        case HAUT :
        if ( m_animation.GetAnim() != &m_TapeH) // On change l'animation
        m_animation.SetAnim(&m_TapeH);
        break;

        case BAS :
        if ( m_animation.GetAnim() != &m_TapeB) // On change l'animation
        m_animation.SetAnim(&m_TapeB);
        break;

        if (m_animation.IsPaused()) //Si il était en pause on le lance
        m_animation.Play();
        }
    }
    if (Walking && Hitting == 0) //Si il marche et ne tape pas
    {

        // On gère l'animation de déplacement en fonction de la direction et en testant les collisions
        // auparavant.
     switch (m_direction)
     {
         case HAUT :

         //CheckCollision(IntRect Perso,int direction)

     if ( m_animation.GetAnim() != &m_Haut)
     m_animation.SetAnim(&m_Haut);

     m_animation.Move(0, -m_speed*ElapsedTime);

      break;

     case BAS :


     if (m_animation.GetAnim() != &m_Bas)
     m_animation.SetAnim(&m_Bas);

     m_animation.Move(0, m_speed*ElapsedTime);

     break;

     case GAUCHE :


     if (m_animation.GetAnim() != &m_Gauche)
     m_animation.SetAnim(&m_Gauche);

     m_animation.Move(-m_speed*ElapsedTime, 0);

     break;

     case DROITE :


     if (m_animation.GetAnim() != &m_Droite)
     m_animation.SetAnim(&m_Droite);

     m_animation.Move(m_speed*ElapsedTime, 0);

     break;
    }
    if (m_animation.IsPaused())
    m_animation.Play();
    m_animation.anim(ElapsedTime);
}

    if (!Walking && Hitting == 0) //Si il ne marche pas et qu'il ne tape pas
    {
        //On arrète le déplacement
        switch (m_direction)
        {
            case HAUT :
            if (m_animation.GetAnim() != &m_Haut)
            m_animation.SetAnim(&m_Haut);
            break;

            case BAS :
            if (m_animation.GetAnim() != &m_Bas)
            m_animation.SetAnim(&m_Bas);
            break;

            case GAUCHE :
            if (m_animation.GetAnim() != &m_Gauche)
            m_animation.SetAnim(&m_Gauche);
            break;

            case DROITE :
            if (m_animation.GetAnim() != &m_Droite)
            m_animation.SetAnim(&m_Droite);
            break;

        }

        if (!m_animation.IsStoped())
        m_animation.Stop();

        m_animation.anim(ElapsedTime);
    }

}
